﻿//滚动的组件

package org.summerTree.components
{

	import flash.display.Sprite;
	import flash.display.Shape;
	import flash.events.*;
	import flash.geom.Rectangle;
	import flash.display.DisplayObjectContainer;
	import flash.text.*;
	public class Slider extends Sprite
	{

		private var bar_control:Sprite;
		private var values:Number=0;
		private var msg:TextField=new TextField();
		private var minValue:Number=0;
		private var maxValue:Number=100;
		public function Slider(contain:DisplayObjectContainer=null,minValue:Number=0,maxValue:Number=100)
		{
			if(contain!=null)
			contain.addChild(this);
			
			this.minValue=minValue;
			this.maxValue=maxValue;
			init();
		}
		
		private function init():void
		{
			//控件底座
			var bar_bottom:Shape=new Shape();
			addChild(bar_bottom);
			bar_bottom.graphics.lineStyle(0);
			bar_bottom.graphics.beginFill(0x666666);
			bar_bottom.graphics.drawRect(0,0,116,16);
			bar_bottom.graphics.endFill();
            var array:Array=new Array();
			
			//设置显示区域
			var format:TextFormat=new TextFormat ();
			format.size=5;
			msg=new TextField();
			msg.border=true;
			msg.setTextFormat(format);
			msg.text=String(this.minValue);
			msg.width=25;
			msg.height=16;
			msg.x=117;
			msg.y=0;
			msg.selectable=false;
			addChild(msg);
		
			
			bar_control=new Sprite();
			addChild(bar_control);
			bar_control.buttonMode=true;
			bar_control.graphics.lineStyle(0);
			bar_control.graphics.beginFill(0xCCCCCC);
			bar_control.graphics.drawRect(0,0,16,16);
			bar_control.graphics.endFill();
			
			
			bar_control.addEventListener(MouseEvent.MOUSE_DOWN,onStaDragHandler);
			bar_control.stage.addEventListener(MouseEvent.MOUSE_UP,onStopDragHandler);
		}
		private function onStaDragHandler(event:MouseEvent):void
		{
			bar_control.stage.addEventListener(MouseEvent.MOUSE_MOVE,onMoveHandler);
			event.currentTarget.startDrag(false,new Rectangle(0,0,100,0));//控制拖动局域		
		}
		
		private function onMoveHandler(event:MouseEvent):void
		{			
			var evt:Event=new Event(Event.CHANGE);
			this.value=bar_control.x*(maxValue-minValue)/100+minValue;//计算元件块位置对于的值
			msg.text=String(Math.round(this.value));
			this.dispatchEvent(evt);
		}
		//停止拖动处理
		private function onStopDragHandler(event:MouseEvent):void
		{
			//msg.text=String(this.value);
			bar_control.stopDrag();
			bar_control.stage.removeEventListener(MouseEvent.MOUSE_MOVE,onMoveHandler);
		}
		//设置拖动的值	
		public function set value(dataValue:Number):void
		{
			this.values=dataValue;
		}
		//返回拖动的值	
		public function get value():Number
		{
			return values;
		}
		//设置对象坐标
		public function move(x:Number,y:Number):void
		{
			this.x=x;
			this.y=y;
		}
	}
}